package praktikumid.w09.p5;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Panel which hold the buttons.
 * @author Ago
 *
 */
public class ButtonPanel extends JPanel {
	/**
	 * Reference to the frame.
	 */
	private MyFrame frame;
	
	/**
	 * Inner class which implements ActionListener.
	 * Used for "draw rect" button.
	 * @author Ago
	 *
	 */
	class DrawRectListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// as you can see,
			// using reference to the frame
			// and from there reference to the draw panel,
			// this call is very simple.
			if (frame != null) {
				frame.getDrawPanel().drawRect(200, 200);
			}
		}
		
	}
	
	/**
	 * Constructor of the panel, which
	 * initializes everything. It also
	 * accepts reference to the frame,
	 * which will be stored in object
	 * member.
	 * @param f Reference to the frame
	 */
	public ButtonPanel(MyFrame f) {
		frame = f;
		setBackground(Color.MAGENTA);
		// we use Y_AXIS, in order to align
		// buttons vertically
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JButton drawOval = new JButton("draw oval");
		JButton drawRect = new JButton("draw rect");
		JButton drawLine = new JButton("draw line");
		
		// let's add listeners to the buttons
		// 1) listener implementation in a separate file
		drawOval.addActionListener(new DrawOvalListener());
		// 2) inner class inside the panel class
		drawRect.addActionListener(new DrawRectListener());
		// 3) anonymous class, which 
		// is declared and instantiated at the same time.
		drawLine.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.getDrawPanel().drawLine(300, 300);
			}
		});
		add(drawOval);
		add(drawRect);
		add(drawLine);
	}
}
